package cat.joronya.supermanager.team;

import android.annotation.TargetApi;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.DragEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnDragListener;
import android.widget.ImageView;
import cat.joronya.supermanager.R;
import cat.joronya.supermanager.Supermanager;
import cat.joronya.supermanager.team.MyTeamDashboardListFragment.PlayerData;
import cat.joronya.supermanager.utils.ImageDownloader;

@TargetApi(16)
public class MyTeamDashboardCourtFragment extends Fragment
{
	public static final String TAG = "supermanager";
	
	protected long idequ;
	protected int day;
	
	Cursor myTeamCursor;
	
	public Drawable defaultDrawable; 
	protected ImageDownloader imageDownloader;
	
	public static MyTeamDashboardCourtFragment newInstance(long idequ, int day)
	{
		MyTeamDashboardCourtFragment fragment = new MyTeamDashboardCourtFragment();
		Bundle args = new Bundle();
		args.putLong("cat.joronya.supermanager.myteams.idequ", idequ);
		args.putInt("cat.joronya.supermanager.myteams.day", day);
		fragment.setArguments(args);
		return fragment;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) 
	{
		View view = inflater.inflate(R.layout.dashboard_court, null);
		
		idequ = getArguments().getLong("cat.joronya.supermanager.myteams.idequ");
		day = getArguments().getInt("cat.joronya.supermanager.myteams.day");
		
		defaultDrawable = getResources().getDrawable(R.drawable.noplayer);
		imageDownloader = new ImageDownloader(getActivity(), Supermanager.PLAYERS, 105);
		
		// avisem q el fragment te menu pq es cridi
        setHasOptionsMenu(true);
        
		return view;
	}
	
	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) 
	{
		super.onViewCreated(view, savedInstanceState);
		
		// recuperar el meu equip per inicialitzar equip
		String selectionTeam = Supermanager.MyTeamDetail.ID_EQU+" =? AND "+Supermanager.MyTeamDetail.DAY+" =? ";
		String[] selectionArgsTeam = new String[]{""+idequ,""+day};
		
		myTeamCursor = getActivity().getContentResolver().query(Supermanager.MyTeamDetail.CONTENT_URI, 
			Supermanager.MyTeamDetail.PROJECTION, 
			selectionTeam, 
			selectionArgsTeam, 
			Supermanager.MyTeamDetail.NUM_POSITION_SORT_ORDER);
		
		while( myTeamCursor.moveToNext() )
		{
			// bindem dades dels meus jugadors a pista
			populateMyTeam();
		}
		
		view.findViewById(R.id.b1).setOnDragListener(new MyDragListener());
		view.findViewById(R.id.b2).setOnDragListener(new MyDragListener());
		view.findViewById(R.id.b3).setOnDragListener(new MyDragListener());
		view.findViewById(R.id.a4).setOnDragListener(new MyDragListener());
		view.findViewById(R.id.a5).setOnDragListener(new MyDragListener());
		view.findViewById(R.id.a6).setOnDragListener(new MyDragListener());
		view.findViewById(R.id.a7).setOnDragListener(new MyDragListener());
		view.findViewById(R.id.p8).setOnDragListener(new MyDragListener());
		view.findViewById(R.id.p9).setOnDragListener(new MyDragListener());
		view.findViewById(R.id.p10).setOnDragListener(new MyDragListener());
		view.findViewById(R.id.p11).setOnDragListener(new MyDragListener());
	}
	
	@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) 
    {
        inflater.inflate(R.menu.dashboard, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }
	
	@Override
    public boolean onOptionsItemSelected(MenuItem item) 
    {
		// Handle item selection
        switch( item.getItemId() ) 
        {
        	default:
        		return super.onOptionsItemSelected(item);
        }
    }
	
	public void populateMyTeam()
	{
		String position = myTeamCursor.getString(18);
		int numPosition = myTeamCursor.getInt(19);
		String playerName = myTeamCursor.getString(26);
		String playerCode = myTeamCursor.getString(3);
		
		Log.d(TAG, "MyTeam player: "+playerName+", position: "+position+", numPosition: "+numPosition);
		
		View playerView;
		switch(numPosition)
		{
			case 1:
				playerView = getView().findViewById(R.id.b1);
				break;
			case 2:
				playerView = getView().findViewById(R.id.b2);
				break;
			case 3:
				playerView = getView().findViewById(R.id.b3);
				break;
			case 4:
				playerView = getView().findViewById(R.id.a4);
				break;
			case 5:
				playerView = getView().findViewById(R.id.a5);
				break;
			case 6:
				playerView = getView().findViewById(R.id.a6);
				break;
			case 7:
				playerView = getView().findViewById(R.id.a7);
				break;
			case 8:
				playerView = getView().findViewById(R.id.p8);
				break;
			case 9:
				playerView = getView().findViewById(R.id.p9);
				break;
			case 10:
				playerView = getView().findViewById(R.id.p10);
				break;
			case 11:
			default:
				playerView = getView().findViewById(R.id.p11);
				break;
		}
		
		// setejem com a tag la position
		playerView.setTag(position);

		// afegim img en background...
		imageDownloader.download(String.format(Supermanager.URL_PHOTO_PLAYER, playerCode), (ImageView)playerView, defaultDrawable);
	}

	// this define your drag listener
	class MyDragListener implements OnDragListener 
	{
		Drawable greenShape = getResources().getDrawable(R.drawable.drop_green);
		Drawable redShape = getResources().getDrawable(R.drawable.drop_red);

		public boolean onDrag(View v, DragEvent event) 
		{
			View view = (View) event.getLocalState();
			switch(event.getAction()) 
			{
				case DragEvent.ACTION_DRAG_STARTED:
					break;
				case DragEvent.ACTION_DRAG_ENTERED:
					if( view != null)
					{
						String dropTag = (String)v.getTag();
						PlayerData tag = (PlayerData)view.getTag();
						Log.d(TAG, "dropTag: "+dropTag+", dragTag: "+tag.position);
						if( dropTag.startsWith("b") && Supermanager.GUARD_POSITION.equals(tag.position) )
							v.setBackground(greenShape);
						else if( dropTag.startsWith("a") && Supermanager.FORWARD_POSITION.equals(tag.position) )
							v.setBackground(greenShape);
						else if( dropTag.startsWith("p") && Supermanager.CENTER_POSITION.equals(tag.position) )
							v.setBackground(greenShape);
						else
							v.setBackground(redShape);
					}
					break;
				case DragEvent.ACTION_DRAG_EXITED:        
					v.setBackground(null);
					break;
				case DragEvent.ACTION_DROP:
					v.setBackground(null);
					if( view != null)
					{
						String dropTag = (String)v.getTag();
						PlayerData tag = (PlayerData)view.getTag();
						//ImageView dropView = (ImageView)v;
						//dropView.setImageDrawable(getResources().getDrawable(tag));
						imageDownloader.download(String.format(Supermanager.URL_PHOTO_PLAYER, tag.cod), (ImageView)v, defaultDrawable);
					}
					break;
				case DragEvent.ACTION_DRAG_ENDED:
					v.setBackground(null);
				default:
					break;
			}
			return true;
		}
	}
}
